﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace ImageCropper
{
    public partial class ImageRectangle : ResizableRectangle
    {
        public ImageRectangle()
        {
            InitializeComponent();
        }

        public BitmapSource ImageSource
        {
            get { return this._image.Source as BitmapSource; }
            set
            {
                this._image.Source = value;
                if (value != null)
                {                    
                    this.Width = value.PixelWidth;
                    this.Height = value.PixelHeight;
                }
                else
                {
                    this.Width = 0;
                    this.Height = 0;
                }
            }
        }

        protected override IEnumerable<ResizableRectangle> SnappableObjects
        {
            get { return this.Surface.Rectangles.Where(r => r is ImageRectangle); }
        }

        private void ResizableRectangle_KeyDown(object sender, KeyEventArgs e)
        {
            _KeyDown(e, false, true);
        }

        private void ResizableRectangle_GotFocus(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "Focused", true);
        }

        private void ResizableRectangle_LostFocus(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "Unfocused", true);
        }

        private void _center_MouseMove(object sender, MouseEventArgs e)
        {
            _Move(e);
        }
    }
}
